package com.gxar.quick.ar.server.base.reference;

import com.gxar.common.exception.APIRuntimeException;
import com.gxar.file.api.client.IFileClient;
import com.gxar.file.api.request.BatchFIleDeleteRequest;
import com.gxar.file.api.request.ProductFileBatchDeleteRequest;
import com.gxar.file.api.response.ProductFileBatchDeleteResponse;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/3 15:42
 */
@Service
@Slf4j
public class FileReference {

    private static final String QUICK_AR = "quick-ar";

    @DubboReference(version = "1.0.0")
    private IFileClient iFileClient;

    /**
     * 批量删除文件
     */
    public void batchFileDelete(Long userId, List<String> fileUrl) {
        BatchFIleDeleteRequest request = new BatchFIleDeleteRequest();
        request.setUserId(userId);
        request.setFileUrl(fileUrl);
        request.setProductName(QUICK_AR);
        try {
            iFileClient.batchFileDelete(request);
        } catch (RpcException e) {
            // 远程删除文件失败
            log.error("用户[{}]，创建预览，删除旧预览文件[{}]失败。{}", userId, String.join(",", fileUrl), e.getMessage(), e);
        }
    }

    /**
     * 批量删除文件
     *
     * @param needDeletedList 需要删除的文件
     * @return 删除失败的文件地址响应
     */
    public List<String> productFileBatchDelete(List<String> needDeletedList) {
        ProductFileBatchDeleteRequest request = new ProductFileBatchDeleteRequest();
        request.setProductName(QUICK_AR);
        request.setFileUrl(needDeletedList);
        try {
            ProductFileBatchDeleteResponse response = iFileClient.productFileBatchDelete(request);
            return response.getFailFile();
        } catch (RpcException e) {
            log.error("系统批量删除文件异常：[{}]", e.getMessage(), e);
        }
        return null;
    }
}
